﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.LoadedPmiTypes.CApLoadedPmiTypesApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using System;
using System.Collections.Generic;
using System.Linq;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui48;


namespace DnProcessSimulateCommands.LoadedPmiTypes;

internal static class CApLoadedPmiTypesApp
{
  public static CUiLoadedPmiTypesFlexGrid Grid(this Node node)
  {
    return ((RowCol) node.Row).Grid as CUiLoadedPmiTypesFlexGrid;
  }

  private static CApTreeNodeData NodeData(this Node node) => node.Key as CApTreeNodeData;

  public static TxPmiType PmiType(this Node node) => node.NodeData().PmiType;

  public static int OrderIndex(this Node node) => node.NodeData().OrderIndex;

  public static void MoveToOppositeGrid(this Node node)
  {
    node.Grid().OppositeGrid();
    if (node.Level == 0)
      CApLoadedPmiTypesApp.MoveBaseNodeToOppositeGrid(node);
    else
      CApLoadedPmiTypesApp.MoveChildNodeToOppositeGrid(node);
  }

  private static void MoveBaseNodeToOppositeGrid(Node node)
  {
    if (!node.Grid().OppositeGrid().NodesList().Exists((Predicate<Node>) (n => TxPmiType.op_Equality(n.PmiType(), node.PmiType()))))
      CApLoadedPmiTypesApp.CopyBaseNodeToOppositeGrid(node);
    if (node.Nodes.Length != 0)
    {
      foreach (Node node1 in node.Nodes)
        node1.MoveToOppositeGrid();
    }
    else
      node.RemoveNode();
  }

  private static void CopyBaseNodeToOppositeGrid(Node node)
  {
    CUiLoadedPmiTypesFlexGrid pmiTypesFlexGrid = node.Grid().OppositeGrid();
    List<Node> all = pmiTypesFlexGrid.NodesList().FindAll((Predicate<Node>) (n => n.Level == 0));
    Node closestNode = CApLoadedPmiTypesApp.GetClosestNode(node, all);
    (closestNode == null ? ((C1FlexGridBase) pmiTypesFlexGrid).Rows.InsertNode(0, 0) : (closestNode.OrderIndex() >= node.OrderIndex() ? closestNode.AddNode((NodeTypeEnum) 7, node.Data) : closestNode.AddNode((NodeTypeEnum) 6, node.Data))).InitNodeData(node);
  }

  private static void MoveChildNodeToOppositeGrid(Node node)
  {
    CUiLoadedPmiTypesFlexGrid pmiTypesFlexGrid = node.Grid().OppositeGrid();
    Node parentNode = node.Parent;
    if (parentNode == null)
      return;
    if (!pmiTypesFlexGrid.NodesList().Exists((Predicate<Node>) (n => TxPmiType.op_Equality(n.PmiType(), parentNode.PmiType()))))
      CApLoadedPmiTypesApp.CopyBaseNodeToOppositeGrid(parentNode);
    Node node1 = pmiTypesFlexGrid.NodesList().Find((Predicate<Node>) (n => TxPmiType.op_Equality(n.PmiType(), parentNode.PmiType())));
    Node closestNode = CApLoadedPmiTypesApp.GetClosestNode(node, ((IEnumerable<Node>) node1.Nodes).ToList<Node>());
    Node node2;
    if (closestNode != null)
    {
      node2 = closestNode.OrderIndex() >= node.OrderIndex() ? closestNode.AddNode((NodeTypeEnum) 7, node.Data) : closestNode.AddNode((NodeTypeEnum) 6, node.Data);
    }
    else
    {
      Node node3 = node1;
      TxPmiType txPmiType = node.PmiType();
      string name = ((TxPmiType) ref txPmiType).Name;
      node2 = node3.AddNode((NodeTypeEnum) 3, (object) name);
    }
    node2.InitNodeData(node);
    node.RemoveNode();
    if (parentNode.Nodes.Length != 0)
      return;
    parentNode.RemoveNode();
  }

  private static Node GetClosestNode(Node node, List<Node> sourceNodes)
  {
    Node closestNode = (Node) null;
    TxFlexGrid txFlexGrid = (TxFlexGrid) node.Grid().OppositeGrid();
    if (sourceNodes.Count > 0)
      closestNode = sourceNodes.Select(n => new
      {
        n = n,
        distance = Math.Abs(n.OrderIndex() - node.OrderIndex())
      }).OrderBy(p => p.distance).First().n;
    return closestNode;
  }

  public static void SetNodeData(this Node node, int index, TxPmiType pmiType)
  {
    CApTreeNodeData capTreeNodeData = new CApTreeNodeData()
    {
      OrderIndex = index,
      PmiType = pmiType
    };
    node.Key = (object) capTreeNodeData;
  }

  public static void InitNodeData(this Node node, Node sourceNode)
  {
    node.Data = sourceNode.Data;
    node.Image = sourceNode.Image;
    node.Level = sourceNode.Level;
    node.Key = sourceNode.Key;
  }
}
